﻿using UnityEngine;
using UnityEngine.Events;
using System.Collections;
using System.Collections.Generic;

namespace linkouqs.Gamekit {

    /// <summary>
    /// https://unity3d.com/cn/learn/tutorials/topics/scripting/events-creating-simple-messaging-system
    /// </summary>
    public sealed class EventManager : MonoSingleton<EventManager> {

        Dictionary<string, UnityEvent> eventDictionary = new Dictionary<string, UnityEvent>();

        public static void AddListener(string eventName, UnityAction listener) {
            UnityEvent thisEvent = null;
            if (Instance.eventDictionary.TryGetValue(eventName, out thisEvent)) {
                thisEvent.AddListener(listener);
            } else {
                thisEvent = new UnityEvent();
                thisEvent.AddListener(listener);
                Instance.eventDictionary.Add(eventName, thisEvent);
            }
        }

        public static void RemoveListener(string eventName, UnityAction listener) {
            UnityEvent thisEvent = null;
            if (Instance.eventDictionary.TryGetValue(eventName, out thisEvent)) {
                thisEvent.RemoveListener(listener);
            }
        }

        public static void TriggerEvent(string eventName) {
            UnityEvent thisEvent = null;
            if (Instance.eventDictionary.TryGetValue(eventName, out thisEvent)) {
                thisEvent.Invoke();
            }
        }
    }
}